package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import data.Blind;
import data.VotingCard;
import data.VotingSet;

public abstract class SignedCardsSelector {
	
	public void saveVoteCardToFile(String path, VotingSet voteSet, String hash) throws IOException{
		File f = new File(path);
		FileOutputStream fos = new FileOutputStream(f);
		List<VotingCard> list=voteSet.getCards();
		for(VotingCard card:list)
			fos.write((card.getVoted().get(0).toString()+"\r\n").getBytes());
		fos.write("!!-startcard-!!".getBytes());
		fos.write(hash.getBytes());
		fos.close();
		
	}
	
	public String selectCard(String candidate, String path) throws IOException{
		File f=new File(path);
		FileInputStream fis=new FileInputStream(f);
		byte[] b = new byte[(int) f.length()];  
		fis.read(b); 
		String msg=new String(b);
		String[] splitted=msg.split("!!--startcard--!!");
		int num=-1;
		String[] splitted2=splitted[0].split("\r\n");
		for(int i=0; i<splitted2.length; i++)
			if(splitted2[i].startsWith(candidate))
				num=i;
		Blind bb=new Blind();
		return bb.pickCard(splitted[1], num);
	}
}
